package com.wh.wisdomsite.xbox.server.controller.ba;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wh.wisdomsite.xbox.ba.entity.SysUserLog;
import com.wh.wisdomsite.xbox.ba.service.ISysUserLogService;
import com.wh.wisdomsite.xbox.common.util.ResManager;
import com.wh.wisdomsite.xbox.pojo.ba.SysUserLogIn;
import com.wh.wisdomsite.xbox.server.manager.UserMapManager;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import com.wh.wisdomsite.xbox.utils.date.DateTool;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 刘旭
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.server.controller.ba
 * @date 2018年10月30日 10:07
 * @Copyright 企业版权所有
 * @SysUserLog 贵州万华科技有限公司Copyright (c) 2018
 */
@Controller
@RequestMapping(value = "/sysUserLog")
public class SysUserLogController {
    private static final Log logger = LogFactory.getLog(SysUserLogController.class);

    @Autowired
    private ISysUserLogService iSysUserLogService;


    /**
     * 跳转页面到用户日志方法
     * @return
     */
    @RequiresPermissions("rules:us_sysUserLog_page")
    @RequestMapping(value = "index.do", method = RequestMethod.GET)
    public String index(){
        return "/ba/sysUserLog/sysUserLogList";
    }

    /**
     * 获取用户日志列表
     * @param response
     * @param in
     */
    @RequiresPermissions("rules:us_sysUserLog_page")
    @RequestMapping(value = "/sysUserLogList.do")
    public void SysUserLogList(HttpServletResponse response, SysUserLogIn in){
        try {
            logger.info("加载用户日志列表-->>开始");
            //设置分页
            if(in.getPage() == 0) in.setPage(1);
            if(in.getRows() == 0) in.setRows(10);
            Page<SysUserLog> page = new Page<SysUserLog>();
            page.setCurrent(in.getPage());
            page.setSize(in.getRows());
            EntityWrapper<SysUserLog> ewSysUserLog = new EntityWrapper<SysUserLog>();
            ewSysUserLog.setEntity(new SysUserLog());
            if(StringUtils.isNotEmpty(in.getUserId())){
                ewSysUserLog.eq("slo.createBy",in.getUserId());
            }
            if(StringUtils.isNotEmpty(in.getUslCode())){
                ewSysUserLog.eq("slo.uslCode",in.getUslCode());
            }
            if(StringUtils.isNotEmpty(in.getStartTime()) && StringUtils.isNotEmpty(in.getEndTime())){
                ewSysUserLog.between("slo.createDate", in.getStartTime(), in.getEndTime());
            }
            ewSysUserLog.orderBy("slo.createDate",false);
            Page<Map<String, Object>> sysUserLogPage = iSysUserLogService.getMapsPage(page, ewSysUserLog);
            JSONObject json = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            if(sysUserLogPage.getRecords().size() > 0){
                for(Map<String, Object> sysUserLog : sysUserLogPage.getRecords()){
                    JSONObject object = new JSONObject(sysUserLog);
                    if(sysUserLog.get("user") != null){
                        object.put("user",new JSONObject(sysUserLog.get("user")));
                    }
                    String uslCode = (String)sysUserLog.get("uslCode");
                    String operatName = getOperatName(uslCode);
                    object.put("operatName",operatName);
                    object.put("createDate", DateTool.format((Date)sysUserLog.get("createDate"), "yyyy-MM-dd HH:mm:ss"));
                    jsonArray.put(object);
                }
            }
            json.put("total", sysUserLogPage.getTotal());
            json.put("rows", jsonArray);
            ResManager.getTextOut(response).print(json);
            logger.info("加载用户日志列表-->>结束");
        }catch (Exception e){
            e.printStackTrace();
            logger.debug("加载用户日志列表-->>Exception" + e.getMessage());
        }
    }

    /**
     * 用户日志详情
     * @param id
     * @param request
     * @return
     */
    @RequiresPermissions("rules:us_sysUserLog_view")
    @RequestMapping("sysUserLogDesc.do")
    public String sysUserLogDesc(String id, HttpServletRequest request) {
        logger.info("用户日志详情-->>开始");
        Map<String , Object> sysUserLog = iSysUserLogService.getMapById(id);
        String createDate = DateTool.format((Date)sysUserLog.get("createDate"), "yyyy-MM-dd HH:mm:ss");
        String uslCode = (String)sysUserLog.get("uslCode");
        String operatName = getOperatName(uslCode);
        request.setAttribute("operatName",operatName);
        request.setAttribute("sysUserLog", sysUserLog);
        request.setAttribute("createDate", createDate);
        logger.info("用户日志详情-->>结束");
        return "ba/sysUserLog/sysUserLogDesc";
    }

    /**
     * 获取创建的用户
     * @param response
     */
    @RequestMapping(value = "/getUserList.do")
    public void getUserList(HttpServletResponse response) {
        JSONArray jsonArray = new JSONArray();
        try {
            List<SysUserLog> sysUserLogList = new ArrayList<SysUserLog>();
            logger.info("获取用户日志用户-->>开始");
            EntityWrapper<SysUserLog> ewSysUserLog = new EntityWrapper<SysUserLog>();
            ewSysUserLog.setEntity(new SysUserLog());
            ewSysUserLog.groupBy("createBy");
            sysUserLogList = iSysUserLogService.selectList(ewSysUserLog);
            for(SysUserLog sysUserLog : sysUserLogList){
                String createId = sysUserLog.getCreateBy();
                String createName = UserMapManager.getUserLoginNameInMap(createId);
                JSONObject obj = new JSONObject();
                obj.put("userId",createId);
                obj.put("userName",createName);
                jsonArray.put(obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取用户日志用户-->>异常Exception " + e.getMessage());
        }
        ResManager.getJsonOut(response).print(jsonArray);
        logger.info("获取用户日志用户-->>结束");
    }

    /**
     * 获取操作名称
     * @param uslCode
     * @return
     */
    public String getOperatName(String uslCode){
        String operatName = "";
        if(StringUtils.isNotEmpty(uslCode)){
            switch (uslCode){
                case "regist":
                    operatName =  "注册";
                    break;
                case "login":
                    operatName = "登录";
                    break;
                case "updatePwd":
                    operatName = "修改密码";
                    break;
                case "loginOut":
                    operatName = "登出";
                    break;
                default:
                    operatName = "操作错误";
            }
        }
        return operatName;
    }

}
